<?php
/**
 * 用户商业逻辑类
 *
 * @author Alen
 * @since 1.0.0 2013-12-11
 */
class SoBizUser extends SoBizBase {
    //put your code here
    
    private $session_id='';
    private $isadmin=false;
    public function __construct($isadmin=false) {
        parent::__construct();
        $this->dm->tablename='tusermember';
        $this->isadmin=$isadmin;
        $this->session_id=$isadmin?'SOADMLOGININFO':'LOGININFO';
    }
    
    /**
     * 更新交易密码
     * 
     * 成功则返回SUCCESS,失败则返回错误消息
     * @param string $uname
     * @param string $password
     * @param string $repassword
     * @return mixed
     * @since 1.0.0 2013-12-11 sutroon
     */
    public function update_tradepassword($uname,$password,$repassword){
	$this->dm->add_param('TradePassword',$password,'交易密码未填写');
	$this->dm->compare_param($password,$repassword,'两次密码不一致');
        return $this->dm->update(sprintf('UserName=\'%s\'',$uname))?'SUCCESS':$this->dm->get_error();	
    }
    
    /**
     * 添加代理会员
     * @param type $uname
     * @param type $password
     * @param type $repassword
     * @param type $referer 推荐人,如果无则为系统管理员
     * @return type
     * @since 1.0.0 2012-12-18
     */
    public function add_agent($uname,$password,$repassword,$referer){        
	$this->dm->add_param('UserName',$uname,'手机号码未填写');
	$this->dm->add_param('Password',$password,'密码未填写');
	$this->dm->compare_param($password,$repassword,'两次密码输入不一致');
	$this->dm->add_param('ReferrerName',$referer,'推荐人无效');
	if($this->dm->has_error()) return $this->dm->get_error ();
        
        // 判断用户是否存在
	if($this->dm->select_value('', 'UserName=\''.$this->dm->UserName.'\'','count(UserName)')) return '用户名['.$uname.']已存在';
        
	$this->dm->add_param('IsAgent',1);
        $this->dm->add_param('State', 1);
	$this->dm->add_param('RenewalExpiresTime',addtime('1 years'));
	$this->dm->add_param('ActiveTime',date('Y-m-d H:i:s'));
	$this->dm->add_param('ActiveIP',get_client_ip());
		
	$result=$this->dm->insert();
	if($result){
            $bizacc=new SoBizFinanceAccount();
            $bizacc->pay_to_agent($uname);
                $bizacc->add_transaction('system',300,'OUT_PREORDER_SEAT');
                $this->dm->update_records('','QuotaNumber=QuotaNumber-1',"UserName='$referer'");
	}
        return $result=='SUCCESS'?$result:$this->dm->get_error();
    }
    
    /**
     * 激活代理
     * @param type $uname
     * @param type $referer
     * @return string
     * @since 1.0.0 2013-12-18
     */
    public function active_agent($uname,$referer){        
	if(!$this->dm->select_value('', "UserName='$referer'",'QuotaNumber')) return '您的预购席位不足，请先预购席位';
	if(!$uname) return '参数有误';
        $user=$this->dm->select_one_row('', "UserName='$uname'",'State,IsAgent');
	if(!$user) return '用户不存在';
	if(!$user['State']) return '用户已被禁用';
	if($user['IsAgent']==1) return '用户已激活，请勿重复激活';
		
        $this->dm->update_records('', 'IsAgent=1,RenewalExpiresTime=\''.addtime('1 years').'\', ActiveTime=\''.date('Y-m-d H:i:s').'\', ActiveIP=\''.get_client_ip().'\'',"UserName='$uname'");
        // 从账户预付款余额中扣除激活费用300
        $bizacc=new SoBizFinanceAccount();
        $bizacc->add_transaction_for_activeorderseat($referer,300);
        // 支付给五级代理提成
	$bizacc->pay_to_agent($uname);
        
        // 预购坐席数量减1,已激活预购席位数量加1
        $this->dm->update_records('','QuotaNumber=QuotaNumber-1,QuotaNumberActived=QuotaNumberActived+1',"UserName='$referer'");
        // 写入到代理激活日志表
        $this->dm->insert_record('tuseragentactivelog','UserName,ActiveType,RenewalExpiresTime,ActiveIP,ActiveYears,ActivingUserName',sprintf('\'%s\',\'prevorder\',\'%s\',\'%s\',%u,\'%s\'',$uname,addtime('1 years'),get_client_ip(),1,$referer));
	return 'SUCCESS';
    }
    
    /**********************************************************************************************
     * 用户账户控制方法(UAC Begin)
     * 
     * 用户登入登出等通用方法
     ***********************************************************************************************/
    
    /**
     * 普通用户登录
     * 
     * @param type $uname
     * @param type $password
     * @return string
     * @since 1.0.0 2013-12-3 sutroon
     */
    public function login_user($uname,$password){
	if(!$uname || !$password) return '表单内容无效';
        $rs=$this->dm->select_one_row('tusermember', 'UserName=\''.$uname.'\'','Password,State');
	if(!$rs) return '用户不存在';	
	if($rs['Password']!=$password) return '密码错误';
	if(!$rs['State'])  return '您的帐号已被禁用';
        $this->set_login_info(array('USER_NAME'=>$uname));
	return 'SUCCESS';
    }
    
    public function login_admin($uname,$password){        
	if(!$this->dm->exists("UserName='sysadmin'"))	$this->dm->insert_record('','UserName,Password,IsAdmin,State',"'sysadmin','Aa123456_',1,1");
        $pwd=$this->dm->select_value('tusermember',sprintf('IsAdmin=1 and UserName=\'%s\'',$uname),'Password');
	if(!$pwd) return '用户名错误';	
	if($pwd!=$password) return '密码错误';	
        $this->set_login_info(array('USER_NAME'=>$uname),true);
        return 'SUCCESS';
    }
    
    /**
     * 保存用户登录凭据
     * @param array $logininfo
     * @since 1.0.0 2013-12-3
     */
    public function set_login_info($logininfo){
        $_SESSION[$this->session_id]=$logininfo;
    }
    
    /**
     * 用户注销登录
     * @return string
     * @since 1.0.0 2013-12-3 sutroon
     */
    public function login_out(){
        unset($_SESSION[$this->session_id]);
        return 'SUCCESS';
    }
    
    /**
     * 获取登录信息
     * 
     * 成功则返回数组
     * @return array
     * @since 2013-12-4 sutroon
     */
    public function get_login_info(){
        return isset($_SESSION[$this->session_id])?$_SESSION[$this->session_id]:'';
    }
    
    /**
     * 获取指定参数的登录信息的值
     * @param string $name
     * @return mixed
     * @since 1.0.0 2013-12-11
     */
    public function get_login_info_param($name){
        $info=$this->get_login_info();
        return $info?$info[$name]:'';        
    }
}
